import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;

import sun.security.krb5.internal.ccache.CCacheInputStream;

import java.awt.BorderLayout;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;


public class MainForm {

	private JFrame frame;
	private JMapViewer mapa;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainForm window = new MainForm();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainForm()
	{
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize()
	{
		frame = new JFrame();
		frame.setBounds(100, 100, 630, 396);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panelSuperior = new JPanel();
		frame.getContentPane().add(panelSuperior, BorderLayout.NORTH);
		
		JPanel panelInferior = new JPanel();
		frame.getContentPane().add(panelInferior, BorderLayout.SOUTH);
		
		final JSlider slider = new JSlider();
		slider.setValue(10);
		slider.setMaximum(19);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0)
			{
				int valor = slider.getValue();
				mapa.setZoom(valor);
			}
		});
		panelInferior.add(slider);
		
		JPanel panelIzquierdo = new JPanel();
		frame.getContentPane().add(panelIzquierdo, BorderLayout.WEST);
		
		JPanel panelDerecho = new JPanel();
		frame.getContentPane().add(panelDerecho, BorderLayout.EAST);
		
		mapa = new JMapViewer();
		
		mapa.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent arg0)
			{
				/*Coordinate c = mapa.getPosition();
				frame.setTitle(c.getLat() + ", " + c.getLon());*/
			}
		});
		mapa.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				slider.setValue( mapa.getZoom() );
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				Point punto = new Point();
				Coordinate coordenada;
				punto = e.getPoint();
				coordenada=mapa.getPosition(punto);
				MapMarkerDot marca = new MapMarkerDot(coordenada);
				mapa.addMapMarker(marca);
				
				System.out.println(coordenada);
			
			}

			
		});
		mapa.addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent arg0)
			{
				slider.setValue( mapa.getZoom() );
			}
		});
		mapa.setZoomContolsVisible(false);
		mapa.setDisplayPositionByLatLon(-34.52233670387107, -58.70013356208801, 12);
		mostrarClientes();
		mostrarCamino();
		
		frame.getContentPane().add(mapa, BorderLayout.CENTER);
	}
	
	// Muestra los clientes
	private void mostrarClientes()
	{
		for(Cliente c: Cliente.getClientes())
		{
			Coordinate coord = new Coordinate(c.latitud, c.longitud);
			MapMarker marker = new MapMarkerDot(c.nombre, coord);
			mapa.addMapMarker(marker);
		}
	}
	
	// Muestra un camino
	private void mostrarCamino()
	{
		ArrayList<Cliente> clientes = Cliente.getClientes();
		ArrayList<Coordinate> lista = new ArrayList<Coordinate>();
		
		for(Cliente c: clientes)
			lista.add(new Coordinate(c.latitud, c.longitud));
		
		MapPolygon poligono = new MapPolygonImpl(lista);
		poligono.getStyle().setColor(Color.RED);
		mapa.addMapPolygon(poligono);
	}
	
	
}
